import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isNil from "lodash/isNil";
var _excluded = ["receiverSourceId"];

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _class, _class2;

/**
 * SystemNoticePublishAddDrawer
 * @date 2019-06-12
 */
import React, { Component } from 'react';
import uuid from 'uuid/v4';
import { Button, Drawer, Form, Select, Spin } from 'antd';
import { Bind } from 'lodash-decorators';
import EditTable from 'components/EditTable';
import Lov from 'components/Lov';
import intl from 'utils/intl';
import { getEditTableData } from 'utils/utils';
import { operatorRender } from 'utils/renderer';
var fullWidthStyle = {
  width: '100%'
};
var SystemNoticePublishAddDrawer = (_dec = Form.create({
  fieldNameProp: null
}), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_Component) {
  _inherits(SystemNoticePublishAddDrawer, _Component);

  var _super = _createSuper(SystemNoticePublishAddDrawer);

  function SystemNoticePublishAddDrawer(props) {
    var _this;

    _classCallCheck(this, SystemNoticePublishAddDrawer);

    _this = _super.call(this, props);
    _this.state = {
      dataSource: [],
      selectedRows: [],
      selectedRowKeys: []
    };
    return _this;
  } // Btn


  _createClass(SystemNoticePublishAddDrawer, [{
    key: "handleAddBtnClick",
    value: function handleAddBtnClick() {
      var _this$state$dataSourc = this.state.dataSource,
          dataSource = _this$state$dataSourc === void 0 ? [] : _this$state$dataSourc;
      this.setState({
        dataSource: [].concat(_toConsumableArray(dataSource), [{
          id: uuid(),
          _status: 'create'
        }])
      });
    }
  }, {
    key: "handleDelBtnClick",
    value: function handleDelBtnClick() {
      var _this$state = this.state,
          _this$state$dataSourc2 = _this$state.dataSource,
          dataSource = _this$state$dataSourc2 === void 0 ? [] : _this$state$dataSourc2,
          _this$state$selectedR = _this$state.selectedRows,
          selectedRows = _this$state$selectedR === void 0 ? [] : _this$state$selectedR;
      this.setState({
        dataSource: dataSource.filter(function (r) {
          return selectedRows.every(function (rd) {
            return rd.id !== r.id;
          });
        }),
        selectedRows: [],
        selectedRowKeys: []
      });
    }
  }, {
    key: "handleSaveBtnClick",
    value: function handleSaveBtnClick() {
      var _this$state$dataSourc3 = this.state.dataSource,
          dataSource = _this$state$dataSourc3 === void 0 ? [] : _this$state$dataSourc3;

      if (dataSource.length > 0) {
        var validationDataSource = getEditTableData(dataSource, ['id', '_status']);

        if (dataSource.length === validationDataSource.length) {
          // 校验通过
          var _this$props = this.props,
              onAddReceiver = _this$props.onAddReceiver,
              record = _this$props.record,
              organizationId = _this$props.organizationId;
          onAddReceiver({
            records: validationDataSource.map(function (rd) {
              var receiverSourceId = rd.receiverSourceId,
                  other = _objectWithoutProperties(rd, _excluded);

              if (_isNil(rd.tenantId)) {
                if (rd.receiverTypeCode === 'ALL') {
                  return _objectSpread(_objectSpread({}, other), {}, {
                    tenantId: organizationId
                  });
                }

                return _objectSpread(_objectSpread({}, other), {}, {
                  tenantId: organizationId,
                  receiverSourceId: rd.receiverSourceId
                });
              }

              if (rd.receiverTypeCode === 'ALL') {
                return other;
              }

              return _objectSpread(_objectSpread({}, other), {}, {
                receiverSourceId: rd.receiverSourceId
              });
            }),
            noticeId: record.noticeId
          });
        }
      } else {
        this.handleClose();
      }
    } // Table

  }, {
    key: "handleRowSelectionChange",
    value: function handleRowSelectionChange(_, selectedRows) {
      this.setState({
        selectedRows: selectedRows,
        selectedRowKeys: selectedRows.map(function (r) {
          return r.id;
        })
      });
    } // Table Record

  }, {
    key: "removeRecord",
    value: function removeRecord(record) {
      var _this$state$dataSourc4 = this.state.dataSource,
          dataSource = _this$state$dataSourc4 === void 0 ? [] : _this$state$dataSourc4;
      this.setState({
        dataSource: dataSource.filter(function (r) {
          return r.id !== record.id;
        })
      });
    }
  }, {
    key: "renderReceiverType",
    value: function renderReceiverType() {
      var _this$props2 = this.props,
          _this$props2$record = _this$props2.record,
          record = _this$props2$record === void 0 ? {} : _this$props2$record,
          isTenantRole = _this$props2.isTenantRole,
          _this$props2$receiver = _this$props2.receiverRecordType,
          receiverRecordType = _this$props2$receiver === void 0 ? [] : _this$props2$receiver; // 通知类型

      if (record.receiverTypeCode === 'NOTIFY') {
        return (isTenantRole ? receiverRecordType.filter(function (item) {
          return item.value !== 'TENANT' && item.value !== 'ALL';
        }) : receiverRecordType.filter(function (item) {
          return item.value !== 'ALL';
        })).map(function (item) {
          return /*#__PURE__*/React.createElement(Select.Option, {
            key: item.value,
            value: item.value
          }, item.meaning);
        });
      }

      if (record.receiverTypeCode === 'ANNOUNCE') {
        return (isTenantRole ? receiverRecordType.filter(function (item) {
          return item.value === 'UNIT' || item.value === 'ALL' || item.value === 'ROLE';
        }) : receiverRecordType.filter(function (item) {
          return item.value === 'TENANT' || item.value === 'UNIT' || item.value === 'ALL' || item.value === 'ROLE';
        })).map(function (item) {
          return /*#__PURE__*/React.createElement(Select.Option, {
            key: item.value,
            value: item.value
          }, item.meaning);
        });
      }
    }
  }, {
    key: "getColumns",
    value: function getColumns() {
      var _this2 = this;

      return [{
        dataIndex: 'receiverTypeCode',
        title: intl.get('cmsg.common.view.type').d('类型'),
        width: 160,
        render: function render(_, record) {
          var form = record.$form;
          return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Form.Item, null, form.getFieldDecorator('receiverTypeCode', {
            rules: [{
              required: true,
              message: intl.get('cpaas.common.validation.notNull', {
                name: intl.get('cmsg.common.view.type').d('类型')
              })
            }]
          })( /*#__PURE__*/React.createElement(Select, {
            onChange: function onChange() {
              form.setFieldsValue({
                receiverSourceId: null
              });
            },
            style: fullWidthStyle
          }, _this2.renderReceiverType()))), form.getFieldDecorator('tenantId')( /*#__PURE__*/React.createElement("div", null)));
        }
      }, {
        dataIndex: 'receiverSourceId',
        title: intl.get('cmsg.notice.model.receive.receiverSource').d('接收方名称'),
        render: function render(receiverSource, record) {
          var _this2$props = _this2.props,
              isTenantRole = _this2$props.isTenantRole,
              organizationId = _this2$props.organizationId;
          var form = record.$form; // 接收方类型,值集：CMSG.NOTICE.RECEIVER_RECORD_TYPE
          // 租户：CPFM.TENANT_ENCRYPT
          // 组织部门：isTenantRole ? 'CMSG.NOTICE_RECEIVER_UNIT': 'CMSG.SITE.NOTICE_RECEIVER_UNIT',
          // 用户组：CIAM.USER_GROUP
          // 用户：CIAM.SITE.USER
          // 角色: isTenantRole ? 'CIAM.TENANT_ROLE': 'CIAM.SITE.ROLE',

          var type = form.getFieldValue('receiverTypeCode');
          var lovCode = {
            TENANT: isTenantRole ? '' : 'CPFM.TENANT_ENCRYPT',
            UNIT: isTenantRole ? 'CMSG.NOTICE_RECEIVER_UNIT' : 'CMSG.SITE.NOTICE_RECEIVER_UNIT',
            USER_GROUP: 'CIAM.USER_GROUP',
            USER: 'CIAM.SITE.USER',
            ROLE: isTenantRole ? 'CIAM.TENANT_ROLE' : 'CIAM.SITE.ROLE'
          }[type];
          var queryParams = {};

          switch (type) {
            case 'TENANT':
              break;

            case 'UNIT':
              break;

            case 'USER_GROUP':
              queryParams.tenantId = organizationId;
              break;

            case 'USER':
              queryParams.organizationId = organizationId;
              break;

            case 'ROLE':
              queryParams.tenantId = organizationId;
              break;

            default:
              break;
          }

          return /*#__PURE__*/React.createElement(Form.Item, null, form.getFieldDecorator('receiverSourceId', {
            rules: [{
              required: type !== 'ALL',
              message: intl.get('cpaas.common.validation.notNull', {
                name: intl.get('cmsg.notice.model.receive.receiverSource').d('接收方名称')
              })
            }]
          })( /*#__PURE__*/React.createElement(Lov, {
            key: lovCode // 全局(ALL) 时不允许编辑
            ,
            disabled: !type || type === 'ALL',
            code: lovCode,
            style: fullWidthStyle,
            onChange: function onChange(_, lovRecord) {
              if (!_isNil(lovRecord.realTenantId)) {
                form.setFieldsValue({
                  tenantId: lovRecord.realTenantId
                });
              }
            },
            queryParams: queryParams
          })));
        }
      }, {
        key: 'action',
        title: intl.get('cpaas.common.button.action').d('操作'),
        width: 80,
        render: function render(_, record) {
          var operators = [];
          operators.push({
            key: 'delete',
            ele: /*#__PURE__*/React.createElement("a", {
              onClick: function onClick() {
                _this2.removeRecord(record);
              }
            }, intl.get('cpaas.common.button.delete').d('删除')),
            len: 2,
            title: intl.get('cpaas.common.button.delete').d('删除')
          });
          return operatorRender(operators);
        }
      }];
    } // Drawer

  }, {
    key: "handleClose",
    value: function handleClose() {
      var onClose = this.props.onClose;
      onClose();
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props3 = this.props,
          _this$props3$visible = _this$props3.visible,
          visible = _this$props3$visible === void 0 ? false : _this$props3$visible,
          _this$props3$createRe = _this$props3.createReceiverLoading,
          createReceiverLoading = _this$props3$createRe === void 0 ? false : _this$props3$createRe;
      var _this$state2 = this.state,
          _this$state2$dataSour = _this$state2.dataSource,
          dataSource = _this$state2$dataSour === void 0 ? [] : _this$state2$dataSour,
          _this$state2$selected = _this$state2.selectedRowKeys,
          selectedRowKeys = _this$state2$selected === void 0 ? [] : _this$state2$selected;
      var rowSelection = {
        selectedRowKeys: selectedRowKeys,
        onChange: this.handleRowSelectionChange
      };
      var columns = this.getColumns();
      return /*#__PURE__*/React.createElement(Drawer, {
        width: 800,
        title: intl.get('cmsg.notice.view.title.createPublishRecord').d('新增发布记录'),
        visible: visible,
        onClose: this.handleClose
      }, /*#__PURE__*/React.createElement(Spin, {
        spinning: createReceiverLoading
      }, /*#__PURE__*/React.createElement("div", {
        className: "table-operator"
      }, /*#__PURE__*/React.createElement(Button, {
        type: "primary",
        onClick: this.handleSaveBtnClick,
        disabled: dataSource.length === 0,
        loading: createReceiverLoading
      }, intl.get('cpaas.common.button.release').d('发布')), /*#__PURE__*/React.createElement(Button, {
        onClick: this.handleDelBtnClick,
        disabled: createReceiverLoading
      }, intl.get('cpaas.common.button.delete').d('删除')), /*#__PURE__*/React.createElement(Button, {
        onClick: this.handleAddBtnClick,
        disabled: createReceiverLoading
      }, intl.get('cpaas.common.button.add').d('新增'))), /*#__PURE__*/React.createElement(EditTable, {
        bordered: true,
        pagination: false,
        rowKey: "id",
        dataSource: dataSource,
        columns: columns,
        rowSelection: rowSelection
      })));
    }
  }]);

  return SystemNoticePublishAddDrawer;
}(Component), (_applyDecoratedDescriptor(_class2.prototype, "handleAddBtnClick", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "handleAddBtnClick"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleDelBtnClick", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "handleDelBtnClick"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSaveBtnClick", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSaveBtnClick"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleRowSelectionChange", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "handleRowSelectionChange"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "removeRecord", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "removeRecord"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "renderReceiverType", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "renderReceiverType"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleClose", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "handleClose"), _class2.prototype)), _class2)) || _class);
export { SystemNoticePublishAddDrawer as default };